home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Advanced I⁄O v2.3 / Advanced i⁄o / arithm_model.cc < prev    next >
Text File  |  1995-05-10  |  3KB  |  100 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *                   Arithmetic Coding
  6.  *               Basics for the model of the data source
  7.  *
  8.  * The functions in the present file are responsible for providing the
  9.  * arithmetic coder with the probability distribution for the data
  10.  * to encode/decode. To be precise, the function should supply the
  11.  * coder with the cumulative frequency count for the any symbol of interest.
  12.  *
  13.  * The present file provides the basic functionality that any input data
  14.  * model is going to need. As a matter of fact, the functions described here
  15.  * are all required by the interface with the arithmetic coder. Any particular
  16.  * model of input source is to be derived from the Input_Data_Model class
  17.  * and inherits all the basics, probably extending it to account for
  18.  * the specific data model.
  19.  *
  20.  * The program assumes the total no. of distinct input symbols
  21.  * (integers) is relatively small, so simple linear arrays can be used
  22.  * for storing and looking up the frequency tables. 
  23.  *
  24.  * $Id: arithm_model.cc,v 2.0 1995/02/07 19:47:46 oleg Exp oleg $
  25.  *
  26.  ************************************************************************
  27.  */
  28.  
  29. #include "arithm.h"
  30.  
  31.  
  32. /*
  33.  *------------------------------------------------------------------------
  34.  *            Constructors and Destructors
  35.  */
  36.  
  37.                 // Constructor
  38. Input_Data_Model::Input_Data_Model(void)
  39. {
  40.   no_indices      = 0;
  41.   EOF_index      = 0;
  42.   Top_freq      = 0;
  43.   frequencies     = 0;
  44.   cum_frequencies = 0;
  45. }
  46.  
  47. void Input_Data_Model::allocate_model(const int no_symbols)
  48. {
  49.   assure(no_symbols > 1,
  50.      "Cannot construct input model for less than 2 symbols");
  51.   no_indices = no_symbols+1;        // Because EOF_index is extra
  52.   EOF_index  = no_indices;
  53.                 // Since the index is always >0, the 0th
  54.                 // element in these arrays is reserved for
  55.                 // the total freq (cum freq)
  56.   frequencies     = new Lword[no_indices+1];
  57.   cum_frequencies = new Lword[no_indices+1];
  58. }
  59.  
  60.                 // Destructor
  61. Input_Data_Model::~Input_Data_Model(void)
  62. {
  63.   assert( frequencies != 0 && cum_frequencies != 0 );
  64.   delete [] frequencies;
  65.   delete [] cum_frequencies;
  66. }
  67.                     // Set the Top_freq that is
  68.                     // required by coder
  69. void Input_Data_Model::agree_on_top_freq(const int top_freq)
  70. {
  71.   assure(top_freq >= 255, 
  72.      "The top value for the cumulative frequency must be at least 255");
  73.   Top_freq = top_freq;
  74.   assure(total_cum_freq() <= Top_freq,
  75.      "Total cum_freq count is larger than the value set up by the codec");
  76. }
  77.  
  78. /*
  79.  *------------------------------------------------------------------------
  80.  *              Some index manipulation functions
  81.  */
  82.                 // Find an index given its cum freq
  83.                 // Note that cum_fr *CAN* be zero!
  84.                 // In fact, cum_fr for EOF_index is
  85.                 // _exactly_ 0, see, say, arithm_modadapt.cc
  86. Index Input_Data_Model::find_index(const Lword cum_fr) const
  87. {
  88.   if( cum_fr > cum_frequencies[0] )
  89.     _error("find_index: cum frequency %d is out of boundaries [0,%d]",
  90.        cum_fr, cum_frequencies[0]);
  91.   Index index;
  92.   for(index=1; cum_frequencies[index] > cum_fr && index <= no_indices;
  93.       index++)
  94.     ;
  95.   if( index > no_indices )    
  96.     _error("Failure to find the index for the cumulative frequency %d",
  97.        cum_fr);
  98.   return index;
  99. }
  100.